package com.chen.controller;

import cn.hutool.core.util.ObjectUtil;
import com.chen.dto.TeacherDTO;
import com.chen.dto.TeacherLoginDTO;
import com.chen.service.TeacherService;
import com.chen.vo.TeacherVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 教师相关接口
 */
@Controller
@RequestMapping("/teacher")
public class TeacherController {

    // 教师相关服务
    @Autowired
    private TeacherService teacherService;

    /**
     * 教师登录
     * @param dto
     * @return
     */
    @PostMapping("/teacherlogin")
    public ModelAndView teacherLogin(TeacherLoginDTO dto, HttpSession session, ModelAndView modelAndView) {
        // 执行登陆逻辑
        TeacherVO teacher = teacherService.teacherLogin(dto);

        if(ObjectUtil.isEmpty(teacher)){
            modelAndView.addObject("error", "账号不存在，或密码错误！");
            modelAndView.setViewName("/admin/login");
            return modelAndView;
        }

        session.setAttribute("adminPower", teacher.getAdminPower());
        session.setAttribute("loginTeacher", teacher);
        modelAndView.setViewName("/admin/suc");

        return modelAndView;
    }

    /**
     * 教师列表
     * @param startPage
     * @param pageShow
     * @return
     */
    @RequestMapping(value="/teachers", method= RequestMethod.GET)
    public ModelAndView getTeachers(
            @RequestParam(value="startPage", required=false, defaultValue="1") Integer startPage,  //当前页码,默认第一页
            @RequestParam(value="pageShow", required=false, defaultValue="10") Integer pageShow /*每页显示数据量，默认10条*/) {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/teachers");

        //计算当前查询起始数据索引
        int startIndex = (startPage-1) * pageShow;
        List<TeacherVO> teachers = teacherService.listPage(startIndex, pageShow);
        model.addObject("teachers", teachers);

        //获取教师总量
        Long teacherTotal = teacherService.findAllCount();
        //计算总页数
        Long pageTotal = 1L;
        if (teacherTotal % pageShow == 0)
            pageTotal = teacherTotal / pageShow;
        else
            pageTotal = teacherTotal / pageShow + 1;
        model.addObject("pageTotal", pageTotal);
        model.addObject("pageNow", startPage);

        return model;
    }

    /**
     * 新增教师信息
     * @param teacherDTO
     * @return
     */
    @PostMapping(value="/save")
    public String save(TeacherDTO teacherDTO) {
        teacherService.saveTeacher(teacherDTO);
        return "redirect:/teacher/teachers";
    }

    /**
     * 修改教师
     * @param teacherId
     * @return
     */
    @GetMapping("/edit/{teacherId}")
    public ModelAndView edit(@PathVariable("teacherId") Integer teacherId) {
        ModelAndView model = new ModelAndView();
        //获取要修改教师
        TeacherVO teacher = teacherService.getTeacherById(teacherId);
        model.setViewName("/admin/teacheredit");
        model.addObject("teacher", teacher);
        return model;
    }

    @PostMapping(value="/update")
    public String update(TeacherDTO teacherDTO) {
        teacherService.updateTeacher(teacherDTO);
        return "redirect:/teacher/teachers";
    }


    /**
     * 删除教师
     * @param teacherId 教师编号
     * @return
     */
    @PostMapping(value="/del/{teacherId}")
    public String delTeacher(@PathVariable("teacherId") Integer teacherId) {
        teacherService.deleteTeacher(teacherId);
        return  "redirect:/teacher/teachers";
    }

    /**
     * 退出登陆
     * @param session
     * @throws IOException
     */
    @RequestMapping("/exitTeacher")
    public String exitTeacher(HttpSession session) {
        session.removeAttribute("loginTeacher");
        session.removeAttribute("adminPower");
        return "/admin/login";
    }


}
